package wyp.library.ui.dialog;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class WypDownloadDialogView extends View {

	public WypDownloadDialogView(Context context) {
		super(context);
		initSubviews();
	}

	public WypDownloadDialogView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initSubviews();
	}

	public WypDownloadDialogView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initSubviews();
	}

	/**
	 * 圆环颜色
	 */
	public static int RING_COLOR = Color.WHITE;

	/**
	 * 半径
	 */
	public static int RADIUS = 30;

	/**
	 * 圆环宽度
	 */
	public static float RING_WIDTH = 7;

	/**
	 * 画笔
	 */
	private Paint mRingPaint;

	/**
	 * 当前进度
	 */
	private float mPercent = 0;

	private void initSubviews() {
		mRingPaint = new Paint();
		mRingPaint.setAntiAlias(true);
		mRingPaint.setColor(RING_COLOR);
		mRingPaint.setStyle(Paint.Style.STROKE);
		mRingPaint.setStrokeWidth(RING_WIDTH);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		int __xCenter = getWidth() / 2;
		int __yCenter = getHeight() / 2;

		float __ringRadius = RADIUS + RING_WIDTH / 2;
		if (mPercent > 0) {
			RectF __rect = new RectF();
			__rect.left = (__xCenter - __ringRadius);
			__rect.top = (__yCenter - __ringRadius);
			__rect.right = __ringRadius * 2 + (__xCenter - __ringRadius);
			__rect.bottom = __ringRadius * 2 + (__yCenter - __ringRadius);
			canvas.drawArc(__rect, -90, (mPercent) * 360, false, mRingPaint);
		}
		super.onDraw(canvas);
	}

	public void setPercent(float __percent) {
		mPercent = __percent;
		postInvalidate();
	}
}
